function [Idx,MidPoints] = ClassStates(StateVar,StateClasses)
% [Idx,MidPoints] = ClassStates(StateVar,StateClasses)
% Inputs:
% StateVar     = Matrix with variables to be classified (obs in row, var in column)
% StateClasses = Matrix with threshold values for classification (one column for each var)
% Output:
% Idx          = Matrix with indices for each observation (obs in rows)
% MidPoints    = Matrix with the MidPoints for each variable (mid points in rows)


nv  = size(StateVar,2);
Idx = nan(size(StateVar));
nq  = size(StateClasses,1);

for v=1:nv
    [~,Idx(:,v)] = histc(StateVar(:,v),StateClasses(:,v));
    % the way histc assings to bins is weird. the last bin will contain only
    % one observation with the highest value.
    % Fix this:
    Idx(Idx(:,v)==nq,v) = (nq-1);
end

MidPoints = nan(nq-1,nv);
for v = 1:nv
    for q = 1:nq-1
        MidPoints(q,v) = (StateClasses(q,v)+StateClasses(q+1,v))/2;
    end
end


end